<template>
  <div>
    <div class="head-container">
      <el-row>
        <el-col :span="8"><span class="title">集客和包兑换</span></el-col>
        <el-col :span="16">
          <span class="content"
                v-if="isLogin">
            您好！{{username}}
            <el-button class="btn"
                       size="mini"
                       @click="logout">注销</el-button>
          </span>
          <span class="content"
                v-else>
            未登录<el-button class="btn"
                       size="mini"
                       type="success"
                       @click="login">登陆</el-button>
          </span>
          <span>
            <el-button type="warning"
                       size="mini"
                       @click="toPc">电脑端</el-button>
          </span>
        </el-col>
      </el-row>

    </div>
    <div class="app-container">
      <el-form :model="query"
               inline>
        <el-form-item label="号码：">
          <el-input v-model="query.tell"
                    size="mini"
                    style="width:10rem"
                    placeholder="请输入号码"
                    clearable></el-input>
        </el-form-item>

        <el-form-item label="是否可办理：">
          <el-select v-model="query.is_valid"
                     style="width:6rem"
                     size="mini"
                     placeholder="请选择"
                     clearable>
            <el-option label="是"
                       value="是"></el-option>
            <el-option label="否"
                       value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="反馈情况：">
          <el-select v-model="query.res"
                     style="width:6rem"
                     size="mini"
                     placeholder="请选择"
                     clearable>
            <el-option label="已办理"
                       value="已办理"></el-option>
            <el-option label="办理失败"
                       value="办理失败"></el-option>
            <el-option label="未反馈"
                       value=""></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间：">
          <el-date-picker v-model="query.create_time"
                          type="daterange"
                          style="width:15rem"
                          size="mini"
                          align="right"
                          unlink-panels
                          range-separator="-"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="success"
                     size="mini"
                     icon="el-icon-search"
                     @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-button @click="handleDialog('create',null)"
                 type="primary"
                 size="mini"
                 icon="el-icon-plus">新增</el-button>
      <el-button type="warning"
                 size="mini"
                 icon="el-icon-goods"
                 @click="queryHandleDialog">查询办理情况</el-button>
    </div>
    <el-table :data="taskData"
              border
              height="700"
              v-loading="loading"
              style="width:100%">
      <el-table-column align="center"
                       label="号码"
                       prop="tell" />

      <el-table-column align="center"
                       label="是否可以办理"
                       prop="is_valid" />
      <el-table-column align="center"
                       label="渠道"
                       prop="chnl" />
      <el-table-column align="center"
                       label="创建时间">
        <template slot-scope="scope">
          {{parseTime(scope.row.create_time)}}
        </template>
      </el-table-column>
      <el-table-column align="center"
                       label="办理人"
                       prop="handler_man">
      </el-table-column>
      <el-table-column align="center"
                       label="创建人工号"
                       prop="creator" />
      <el-table-column align="center"
                       label="创建人姓名"
                       prop="nick_name" />
      <el-table-column align="center"
                       label="反馈情况">
        <template slot-scope="scope">
          <span>
            <el-tag v-if="scope.row.res === '' || scope.row.res === null"
                    type="warning">未反馈</el-tag>
            <el-tag v-if="scope.row.res !== ''"
                    type="success">{{scope.row.res}}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       prop="is_freeze"
                       label="是否冻结">

      </el-table-column>
      <el-table-column align="center"
                       prop="is_convert"
                       label="是否兑换">
      </el-table-column>
      <el-table-column align="center"
                       width="250"
                       label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="warning"
                       icon="el-icon-edit"
                       size="mini"
                       @click="handleDialog('update',scope.row)">号码反馈</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page.sync="page"
                   :page-size="size"
                   layout="total, prev, pager, next"
                   :total="total">
    </el-pagination>
    <handle-res-num :uuid="uuid"
                    :handleResDialog="handleResDialog"
                    @handleDialogClose="handleDialogClose"></handle-res-num>
    <el-dialog title="号码编辑"
               :visible.sync="editDialog"
               width="100%">
      <el-form :model="form"
               ref="form"
               :rules="rules">
        <el-form-item label="号码："
                      prop="tell">
          <el-input v-model="form.tell"
                    :disabled="type === 'update'"
                    placeholder="请输入号码"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="办理人："
                      prop="handler_man">
          <el-input v-model="form.handler_man"
                    :disabled="type === 'update'"
                    placeholder="请输入办理人"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="渠道："
                      v-if="type === 'update'">
          <el-input v-model="form.chnl"
                    placeholder="请输入渠道"
                    :disabled="type === 'update'"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="是否冻结："
                      v-if="type === 'update'">
          <el-select v-model="form.is_freeze"
                     placeholder="请选择"
                     clearable>
            <el-option label="是"
                       value="是"></el-option>
            <el-option label="否"
                       value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否兑换："
                      v-if="type === 'update'">
          <el-select v-model="form.is_convert"
                     placeholder="请选择"
                     clearable>
            <el-option label="是"
                       value="是"></el-option>
            <el-option label="否"
                       value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success"
                     @click="submit"
                     :loading="uploadLoading"
                     size="small">提交</el-button>
          <el-button type="info"
                     @click="editDialog = false"
                     size="small">返回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import HeadTab from './components/headTab.vue'
import { parseTime } from '@/utils/index'
import { getAllNumber, addNumber, updateNumber } from '@/api/mng'
import HandleResNum from './components/handleResNum.vue'
const defaultForm = { res: "" }
export default {
  name: 'MobilePage',
  components: {
    HeadTab,
    HandleResNum
  },
  computed: {
    username() {
      if (window.sessionStorage.getItem('user')) {
        const user = JSON.parse(window.sessionStorage.getItem('user'))
        return user.username
      } else {
        return ''
      }
    },
    isLogin() {
      return window.sessionStorage.getItem('uuid') ? true : false
    }
  },
  data() {
    return {
      handleResDialog: false,
      isValid: '',
      resForm: {
        res: '已反馈',
        id: ''
      },
      query: {
        tel: '',
        is_valid: '',
        create_time: [],
        res: ''
      },
      form: {
        chnl: ''
      },
      uploadLoading: false,
      editDialog: false,
      role: window.sessionStorage.getItem('role'),
      uuid: window.sessionStorage.getItem('uuid'),
      loading: false,
      taskData: [],
      page: 0,
      size: 10,
      total: 0,
      resList: [],
      type: '',
      rules: {
        tell: [{ required: true, message: '请填写号码', trigger: 'blur' },
        { min: 11, max: 11, message: '请输入11位号码！', trigger: 'blur' }],
        handler_man: [{ required: true, message: '请填写办理人', trigger: 'blur' }]
      },

    }
  },
  created() {
    this.initData(0)
  },
  methods: {
    queryHandleDialog() {
      this.handleResDialog = true
    },
    handleDialogClose(val) {
      this.handleResDialog = false
    },
    toPc() {
      this.$router.push('/')
    },
    logout() {
      window.sessionStorage.removeItem('uuid')
      window.sessionStorage.removeItem('user')
      window.sessionStorage.removeItem('role')
      this.$router.push('/login')
    },
    login() {
      this.$router.push('/login')
    },
    parseTime,
    handleQuery() {
      if (this.tell === null || this.tell === '') {
        this.$message.warning('请输入号码')
      } else {
        this.initData(0)
      }
    },
    handleDialog(type, row) {
      this.editDialog = true
      this.type = type
      switch (type) {
        case 'create':
          this.form = JSON.parse(JSON.stringify(defaultForm))

          // this.form.chnl = user.chnl_name
          break;
        case 'update':
          this.form = row
          break;
        default:
          break;
      }
    },
    initData(page) {
      this.loading = true
      const params = {
        uuid: this.uuid,
        page: page,
        size: this.size,
        tell: this.query.tell || 'null',
        is_valid: this.query.is_valid || 'null',
        create_time_start: this.query.create_time !== null ? this.query.create_time[0] || 'null' : 'null',
        create_time_end: this.query.create_time !== null ? this.query.create_time[1] || 'null' : 'null',
        res: this.query.res || ''
      }
      getAllNumber(params).then(res => {
        if (res.publicData.retInfo.retType === 0) {
          this.taskData = res.data.results
          this.total = res.data.count
        }
        this.loading = false
      }).catch(e => {
        this.$notify.error('接口超时')
        this.loading = false
      })
    },
    submit() {
      const user = JSON.parse(window.sessionStorage.getItem('user'))
      let params = {
        uuid: this.uuid,
        tell: this.form.tell,
        handler_man: this.form.handler_man || '',
        chnl: user.chnl_name,
        res: this.form.res || '',
        is_freeze: this.form.is_freeze || '',
        is_convert: this.form.is_convert || '',
      }
      this.uploadLoading = true
      if (this.type === 'create') {
        this.$refs['form'].validate(valid => {
          if (valid) {
            addNumber(params).then(res => {
              if (res.publicData.retInfo.retType === 0) {
                this.$alert(`${res.data.results}`, '新增成功', {
                  confirmButtonText: '确定'
                });
              } else if (res.publicData.retInfo.retType === 1) {
                this.$alert(`${res.data.results}:${res.data.tell}`, '新增无效', {
                  confirmButtonText: '确定'
                });
              }
              this.initData(0)
              this.editDialog = false
              this.uploadLoading = false
            }).catch(e => {
              this.$notify.error('接口超时')
              this.editDialog = false
              this.uploadLoading = false
            })
          } else {
            this.uploadLoading = false
            return false
          }
        })

      } else if (this.type === 'update') {
        params.id = this.form.id
        console.log(params)
        updateNumber(params).then(res => {
          if (res.publicData.retInfo.retType === 0) {
            this.$notify.success('更改成功')

          }
          this.uploadLoading = false
          this.initData(0)
          this.editDialog = false
        }).catch(e => {
          this.uploadLoading = false
          this.$notify.error('接口超时')
          this.editDialog = false
        })
      }

    },
    handleSizeChange(val) { },
    handleCurrentChange(page) {
      this.initData(page - 1)
      this.page = page
    },
  },
}
</script>

<style lang="scss" scoped>
.head-container {
  width: 100%;
  height: 80px;
  text-align: left;
  color: #fff;
  background: #2d89f0;
  span {
    display: inline-block;
    line-height: 80px;
    margin-left: 3%;
  }
  .title {
    font-size: 1rem;
  }
}
</style>